import { login, logout, register, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
    token: getToken(),
    name: '',
    avatar: '',
    permissions: []
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_PERMISSIONS: (state, permissions) => {
        state.permissions = permissions
    }
}

const actions = {
    // user login
    login({ commit }, userInfo) {
        const { username, password } = userInfo
        return new Promise((resolve, reject) => {
            login({ username: username.trim(), password: password, client_id: '2', client_secret: 'Bru8MeUc4F2eZVwMsxiJI3rRmaDY0jMZ3ZfbQAMn', grant_type: 'password' }).then(response => {
                const data = response
                commit('SET_TOKEN', data.access_token)
                setToken(data.access_token)
                console.log(getToken())
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    getInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            getInfo(state.token).then(response => {
                const data = response

                if (!data) {
                    reject('Verification failed, please Login again.')
                    return
                }
                const { permissions, name, avatar } = data
                if (!permissions || permissions.length <= 0) {
                    reject('getInfo: permissions must be a non-null array!')
                    return
                }

                commit('SET_PERMISSIONS', permissions)
                commit('SET_NAME', name)
                commit('SET_AVATAR', avatar)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // user logout
    logout({ commit, state }) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                commit('SET_TOKEN', '')
                commit('SET_PERMISSIONS', [])
                removeToken()
                resetRouter()
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    register({ commit }, userInfo) {
        const { username, email, password, confirm } = userInfo
        return new Promise((resolve, reject) => {
            register({
                username: username.trim(),
                email: email.trim(),
                password: password,
                confirm: confirm
            }).then(response => {
                const { access_token, permissions, name, avatar } = response
                commit('SET_TOKEN', access_token)
                commit('SET_PERMISSIONS', permissions)
                commit('SET_NAME', name)
                commit('SET_AVATAR', avatar)
                setToken(access_token)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // remove token
    resetToken({ commit }) {
        return new Promise(resolve => {
            commit('SET_TOKEN', '')
            commit('SET_PERMISSIONS', [])
            removeToken()
            resolve()
        })
    }

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
